package com.spring.boot.demo.config;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author ZhuYouBin
 * @version 1.0.0
 * @Date: 2022/10/29 22:48
 * @Description 自定义自动配置的类
 */
// 启用配置属性
@EnableConfigurationProperties(value = CustomProperties.class)
// 自动配置的条件: 这里表示当配置文件中，custom.starter.enabled=true 的时候，才会进行自动配置
@ConditionalOnProperty(prefix = "custom.starter", value = "enabled", matchIfMissing = true)
@Configuration
public class CustomAutoConfiguration {

    @Bean
    // 这个注解表示: 当项目里面没有 DemoService.class 对象，才会自动装配
    @ConditionalOnMissingBean(DemoService.class)
    public DemoService demoService() {
        // 创建 DemoService 对象
        return new DemoService();
    }

}
